import requests
from apiAutomation import app
from apiAutomation.api.login import LoginApi


class AdvertiseApi:

    def __init__(self, token):
        # 查询广告的url地址
        self.query_image_url = app.BATH_URL + "/adver/queryAll"
        # 上传图片地址
        self.upload_image_url = app.BATH_URL + "/adver/upload"
        # 添加广告地址
        self.add_advertise_url = app.BATH_URL + "/adver/addAdvertise"
        # 修改广告地址
        self.update_adress_url = app.BATH_URL + "/adver/updateAdvertise"

        # 请求头
        self.headers_dict_json = {
            "content-type": "applocation/json",
            "token": token
        }
        self.headers_dict_form = {
            "content-type": "multipart/form-data",
            "token": token
        }

    # 查询广告接口
    def query_image(self, pageNum, pageSize):
        # 请求体数据
        data = {
            "pageNum": pageNum,
            "pageSize": pageSize
        }

        return requests.post(url=self.query_image_url, headers=self.headers_dict_json, json=data)

    # 上传图片接口
    def upload_image(self):
        # 上传图片的文件路径和格式
        files = {'file': ("11.jpeg", open(r'C:\Users\dell\Desktop\image\11.jpeg', 'rb'), 'image/jpeg')}
        return requests.post(url=self.upload_image_url, files=files)

    # 添加广告接口
    def add_advertise(self, img_adress, regions):
        data = {
            "picture": img_adress,
            "regions": regions
        }
        return requests.post(url=self.add_advertise_url, headers=self.headers_dict_json, json=data)

    # 修改广告地址
    def update_advertise(self, advertise_id, img_adress, regions):
        data = {
            "advertise_id": advertise_id,
        }
        # 判断要不要传非必填参数username
        if img_adress is not None:
            data["picture"] = img_adress
        # 判断要不要传非必填参数password
        if regions is not None:
            data["regions"] = regions
        return requests.post(url=self.update_adress_url, headers=self.headers_dict_json, json=data)


if __name__ == '__main__':
    b = LoginApi()
    token = b.get_token()
    a = AdvertiseApi(token)
    print(a.upload_image().text)
    print(a.query_image(1, 10).json())
    print(a.update_advertise("ad000003", "http://192.168.1.176:8080/imageCache/adv0.5308262951879211624875311150",[57, 56]).json())
